// 共阴极数码管
int a = 2;      // 定义数字接口2 连接 a 段数码管
int b = 3;      // 定义数字接口3 连接 b 段数码管
int c = 4;      // 定义数字接口4 连接 c 段数码管
int d = 5;      // 定义数字接口5 连接 d 段数码管
int e = 6;      // 定义数字接口6 连接 e 段数码管
int f = 7;      // 定义数字接口7 连接 f 段数码管
int g = 8;      // 定义数字接口8 连接 g 段数码管
int dp = 9;     // 定义数字接口9 连接 dp 段数码管
int num1 = 10;  // 定义数字接口10 选中第一个数码管
int num2 = 11;  // 定义数字接口11 选中第二个数码管
int num3 = 12;  // 定义数字接口12 选中第三个数码管
int num4 = 13;  // 定义数字接口13 选中第四个数码管
int num = 0;    // 切换数码管的变量

void setup() {
  int i;  // 定义变量
  for (i = 2; i <= 13; i++)
    pinMode(i, OUTPUT);  // 设置 2~13 引脚为输出模式
}

void loop() {
  num++;  // 切换数码管变量，四个数码管循环显示
  if (num > 4) {
    num = 1;
  }
  if (num == 1) {
    // 选中第一个数码管
    digitalWrite(num1, LOW);
    digitalWrite(num2, HIGH);
    digitalWrite(num3, HIGH);
    digitalWrite(num4, HIGH);
  } else if (num == 2) {
    // 选中第二个数码管
    digitalWrite(num1, HIGH);
    digitalWrite(num2, LOW);
    digitalWrite(num3, HIGH);
    digitalWrite(num4, HIGH);
  } else if (num == 3) {
    // 选中第三个数码管
    digitalWrite(num1, HIGH);
    digitalWrite(num2, HIGH);
    digitalWrite(num3, LOW);
    digitalWrite(num4, HIGH);
  } else if (num == 4) {
    // 选中第四个数码管
    digitalWrite(num1, HIGH);
    digitalWrite(num2, HIGH);
    digitalWrite(num3, HIGH);
    digitalWrite(num4, LOW);
  }
  digital_0();  // 显示数字0
  delay(1000);  // 延迟1s
  digital_1();  // 显示数字1
  delay(1000);  // 延迟1s
  digital_2();  // 显示数字2
  delay(1000);  // 延迟1s
  digital_3();  // 显示数字3
  delay(1000);  // 延迟1s
  digital_4();  // 显示数字4
  delay(1000);  // 延迟1s
  digital_5();  // 显示数字5
  delay(1000);  // 延迟1s
  digital_6();  // 显示数字6
  delay(1000);  // 延迟1s
  digital_7();  // 显示数字7
  delay(1000);  // 延迟1s
  digital_8();  // 显示数字8
  delay(1000);  // 延迟1s
  digital_9();  // 显示数字9
  delay(1000);  // 延迟1s
}

// 显示数字0
void digital_0(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
  digitalWrite(dp, LOW);
}


// 显示数字1
void digital_1(void) {
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp, LOW);
}

// 显示数字2
void digital_2(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}

// 显示数字3
void digital_3(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}

// 显示数字4
void digital_4(void) {
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}

// 显示数字5
void digital_5(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}

// 显示数字6
void digital_6(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}

// 显示数字7
void digital_7(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp, LOW);
}

// 显示数字8
void digital_8(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}

// 显示数字9
void digital_9(void) {
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp, LOW);
}